<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/admin/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>宠物管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索区域 -->
    <el-row :gutter="20" class="seacher">
      <el-col :span="8">
        <el-input v-model="keywork" placeholder="输入宠物名🐈‍⬛" clearable @clear="getPetList">
          <el-button slot="append" icon="el-icon-search" @click="searchForPet"></el-button>
        </el-input>
      </el-col>
      <!-- 添加区域 -->
      <el-col :span="4">
        <el-button type="primary" @click="insertVisible = true">新增宠物</el-button>
      </el-col>
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="petList" border style="width: 100%" class="tab">
      <el-table-column prop="No" label="查询号码"> </el-table-column>
      <el-table-column prop="url" label="图片">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.url"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="宠物名字"> </el-table-column>
      <el-table-column prop="sex" label="性别"> </el-table-column>
      <el-table-column prop="birthday" label="生日">
        <template slot-scope="scope">{{ $toDate(scope.row.birthday).substr(0, 10) }}</template>
      </el-table-column>
      <el-table-column prop="place" label="出生地"> </el-table-column>
      <el-table-column prop="breed" label="颜色"> </el-table-column>
      <el-table-column prop="blood" label="血统书"> </el-table-column>
      <el-table-column prop="price" label="价格"> </el-table-column>
      <el-table-column prop="features" label="特征"> </el-table-column>
      <el-table-column prop="operation" label="操作">
        <template slot-scope="scope" class="opt">
          <!-- 添加本地图片区域 -->
          <div>
            <el-tooltip class="item" effect="dark" content="添加图片" placement="top-start">
              <el-button type="success" icon="el-icon-plus" size="mini" @click="upload(scope.row.No)"></el-button>
            </el-tooltip>
          </div>
          <!-- 修改按钮 -->
          <div>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="delPet(scope.row.No)"></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[5]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <!-- 用户信息修改框 -->
    <!-- 修改对话框 -->
    <el-dialog title="新增宠物" :visible.sync="insertVisible">
      <el-form :model="editForm">
        <el-form-item label="宠物名：" :label-width="formLabelWidth">
          <el-input v-model.trim="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="性别：" :label-width="formLabelWidth">
          <el-input v-model.trim="editForm.sex" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="出生地：" :label-width="formLabelWidth">
          <el-input v-model.trim="editForm.place" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="颜色：" :label-width="formLabelWidth">
          <el-input v-model.trim="editForm.breed" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="血统书：" :label-width="formLabelWidth">
          <el-input v-model.trim="editForm.blood" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="价格：" :label-width="formLabelWidth">
          <el-input v-model.number="editForm.price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="网络上传?" :label-width="formLabelWidth">
          <el-switch v-model="value" active-color="#13ce66" inactive-color="#ff4949"> </el-switch>
        </el-form-item>
        <el-form-item label="网络图片：" :label-width="formLabelWidth" v-if="value">
          <el-input v-model.trim="editForm.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="分类：1狗2猫" :label-width="formLabelWidth">
          <el-input v-model.number="editForm.sort" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="特征：" :label-width="formLabelWidth">
          <el-input v-model.trim="editForm.features" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="insertVisible = false">取 消</el-button>
        <el-button type="primary" @click="insertPet">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 上传图片弹层 -->
    <el-dialog title="上传图片" :visible.sync="ImgFormVisible">
      <el-upload action="" :on-change="handleelchange" :auto-upload="false" list-type="picture-card">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-button type="primary" @click="imgUpload">上传图片</el-button>
    </el-dialog>
  </div>
</template>

<script>
import { getPetList, insertPet, delPet, localPicture } from '@/api/petmanage'
import { searchPet } from '@/api/pet'
export default {
  name: 'PetManage',
  data() {
    return {
      petList: [],
      // 搜索框
      keywork: '',
      value: true,
      editForm: {
        name: '',
        sex: '',
        place: '上海市',
        breed: '',
        blood: '无',
        price: 0,
        url: '',
        sort: 1,
        features: '',
      },
      queryInfo: {
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      insertVisible: false,
      formLabelWidth: '100px',
      // 图片弹层显示与隐藏
      ImgFormVisible: false,
      No: null,
    }
  },
  created() {
    this.getPetList()
  },
  methods: {
    async getPetList() {
      const result = await getPetList({ pagenum: this.queryInfo.pagenum })
      if (result.status != 0) return this.$message.error(result.message)
      this.petList = result.data
      this.total = result.total.total
    },
    searchForPet() {
      searchPet({ name: this.keywork }).then((result) => {
        this.petList = result.data
      })
    },
    async insertPet() {
      const result = await insertPet({ ...this.editForm })
      if (result.status !== 0) return this.$message.error(result.message)
      this.$message.success(result.message)
      this.insertVisible = false
      this.editForm = {
        name: '',
        sex: '',
        place: '上海市',
        breed: '',
        blood: '无',
        price: 0,
        url: '',
        sort: 1,
        features: '',
      }
      this.getPetList()
    },
    // 分页区的当前页数据显示的条数
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getPetList()
    },
    // 当前页面值
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getPetList()
    },
    async delPet(No) {
      // console.log(No)
      const confirmResult = await this.$confirm('此操作将永久删除该"宠物", 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).catch((err) => err)

      if (confirmResult !== 'confirm') return

      const result = await delPet({ No })
      console.log(result)
      if (result.status !== 0) return this.$message.error('删除失败！')
      this.$message.success('删除成功！')
      this.getPetList()
    },
    // 本地图片上传
    handleelchange(file, fileList) {
      console.log(file, fileList)
      const formdata = new FormData()
      formdata.append('No', this.No)
      fileList.map((item) => {
        // fileList本来就是数组，就不用转为真数组了
        formdata.append('file', item.raw) // 将每一个文件图片都加进formdata
      })
      localPicture(formdata).then((result) => {
        if (result.status !== 0) return this.$message.error(result.message)
        this.$message.success(result.message)
      })
    },
    upload(No) {
      this.No = No
      this.ImgFormVisible = true
    },
    // 上传图片成功关闭弹层刷新网页
    imgUpload() {
      this.ImgFormVisible = false
      window.location.reload()
    },
  },
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 40px;
}
.seacher {
  margin-top: 20px;
}
// 上传图片框样式
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.opt {
  display: flex;
  flex-direction: column;
}
</style>
